package com.wenbo.fish.service.impl;

import com.wenbo.fish.entity.SpiderInfo;
import com.wenbo.fish.enums.SpiderServiceEnum;
import com.wenbo.fish.service.AbstractSpiderService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author changwenbo
 * @date 2024/12/31 14:42
 */
@Service
public class TiebaSpiderServiceImpl extends AbstractSpiderService {
    private static final Logger log = LoggerFactory.getLogger(TiebaSpiderServiceImpl.class);

    @Override
    protected List<SpiderInfo> parseData(String body) {
        try {
            Document doc = Jsoup.parse(body);
            // 标题
            Elements titles = doc.select(".topic-top-item-desc");
            // 链接
            Elements urls = doc.select(".topic-text");
            List<SpiderInfo> list = new ArrayList<>();
            for (int i = 0; i < titles.size(); i++) {
                SpiderInfo spiderInfo = new SpiderInfo(titles.get(i).text().trim(), urls.get(i).attr("href"));
                list.add(spiderInfo);
            }

            return list;
        } catch (Exception e) {
            log.error("TiebaSpiderServiceImpl parseData error e = {}", e.getMessage(), e);
        }

        return EMPTY_LIST;
    }

    @Override
    public SpiderServiceEnum getServiceName() {
        return SpiderServiceEnum.TIE_BA;
    }

}
